﻿/*
 * Copyright 2010 Marc Cesarine
 * 
 * This file is part of Ssh Commander.

 * Ssh Commander is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * Ssh Commander is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Ssh Commander.  If not, see <http://www.gnu.org/licenses/>.
 */

using System.Windows.Controls;
using System.Threading;
using System;

namespace SshCommander
{
    /// <summary>
    /// Logique d'interaction pour Connectivity.xaml
    /// </summary>
    public partial class Connectivity : UserControl
    {
        private ServersList serversList_v;
        private delegate void statuslabeldelegate_v(string label);

        public Connectivity(ref ServersList serversList)
        {
            InitializeComponent();
            serversList_v = serversList;
        }

        private void ConnectivityButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ThreadPool.QueueUserWorkItem(serversList_v.ServersConnectivityThreadPoolCallback);
        }

        /*public void ServersConnectivityThreadPoolCallback(Object threadContext)
        {
            //Tableau des ResetEvent pour connaître l'état d'avancement des threads
            ManualResetEvent[] doneEvents = new ManualResetEvent[serversList_v.serversList.Count];

            //On fait un thread par test à effectuer
            //for (int i = 0; i < serversList_v.serversList.Count; i++)
            int i = 0;
            foreach (Server server in serversList_v.serversList)
            {
                //On initialise le ResetEvent du thread
                doneEvents[i] = new ManualResetEvent(false);

                server.doneevent = doneEvents[i];
                //On crée l'objet Server Threadé
                //Server Server = new Server(doneEvents[i]);
                //Server.Name = ServeursList[i].Name;
                //Server.Connectivite = ServeursList[i].Connectivite;
                //Server.Port = ServeursList[i].Port;

                if (server.isTunneled)
                {
                    //On lance le traitement dans un thread spécifique (utilisation d'un pool de threads)
                    ThreadPool.QueueUserWorkItem(server.ConnectivityTunnelThreadPoolCallback, doneEvents[i]);
                }
                else
                {
                    //On lance le traitement dans un thread spécifique (utilisation d'un pool de threads)
                    ThreadPool.QueueUserWorkItem(server.ConnectivityThreadPoolCallback, doneEvents[i]);
                }
                i++;
            }

            // Wait for all threads in pool to finish...
            WaitHandle.WaitAll(doneEvents);
        }*/
    }
}
